<template>
	<view class="confirm_give">
		<uv-navbar placeholder autoBack bgColor="#CA1C1D">
			<template v-slot:left>
				<view class="left_slot">
					<image src="@/static/images/back.png"></image>
				</view>
			</template>
			<template v-slot:center>
				<view class="nav_title">确认订单</view>
			</template>
		</uv-navbar>

		<scroll-view scroll-y class="scroll_view">
			<view class="address" @click="router('/pages/my/address?is=1')">
				<image src="@/static/images/zb.png" class="zb"></image>
				<view class="address_view">
					<view class="address_info">
						<view class="name">{{region.name}}</view>
						<view class="phone">{{region.phone}}</view>
					</view>
					<view class="address_deta">{{region.region_text}}</view>
				</view>
				<image src="@/static/images/r1.png" class="next"></image>
			</view>
			<view class="goods_info">
				<view class="goods_box">
					<image :src="data_info.img" class="cover"></image>
					<view class="goods_view">
						<view class="goods_deta">
							<view class="name">{{data_info.name}}</view>
							<view class="synopsis">{{data_info.synopsis}}</view>
						</view>
						<view class="price">
							<view class="unit">￥</view>
							<view class="value">{{data_info.price}}</view>
						</view>
					</view>
				</view>
				<!-- <view class="leave">
					<view class="key">留言</view>
					<input type="text" placeholder="请输入您的留言">
				</view> -->
			</view>
			<view class="cell_view">
				<view class="item">
					<view class="key">数量</view>
					<uv-number-box step="1" min="1" :max="data_info.stock" disabledInput integer v-model="form.number"
						@overlimit="overlimit"></uv-number-box>
				</view>
				<view class="item">
					<view class="key">运费</view>
					<view class="value">包邮</view>
				</view>
			</view>
			<view class="other">
				<view class="other_head">
					<image src="@/static/images/99.png"></image>
					<view>填写祝福语:</view>
				</view>
				<uv-textarea v-model="form.message" border="none" count maxlength='15'
					placeholder="请输入内容"></uv-textarea>
				<!-- <view class="nikename">
					<view class="key">匿名：</view>
					<input type="text" placeholder="写下你对她的亲昵">
				</view> -->
			</view>
		</scroll-view>
		<!-- 	<view class="footer">
			<view class="number">
				<view class="key">数量：</view>
				<view class="value">1</view>
			</view>
			<view class="submit">立即赠送</view>
		</view> -->
		<view class="footer">
			<view class="submit" @click="submit">立即赠送</view>
		</view>
	</view>
</template>

<script setup>
	import {
		underwriteUserInfo,
		underwriteReceive
	} from "@/request/api";
	import useWechat from "@/hooks/useWechat.js";
	import useBase from "@/hooks/useBase.js";
	const {
		router
	} = useBase();
	import {
		onLoad,
		onShow,
		onUnload
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive,
		shallowRef
	} from "vue";
	import {
		storeToRefs
	} from 'pinia';
	import useStore from "@/stores/index.js";
	const {
		useAddressStore
	} = useStore();
	let {
		address
	} = storeToRefs(useAddressStore);

	const region = ref({
		name: "",
		phone: "",
		region_text: "",
	});
	const form = reactive({
		address_id: "",
		number: 1,
		message: ""
	});
	const data_info = ref({});

	const id = ref('');
	const getDeta = async () => {
		let res = await underwriteUserInfo({
			id: id.value
		});
		data_info.value = res?.data?.product;
		data_info.value.stock = res?.data?.stock;
		if (!address.value) {
			address.value = res.address;
		}
		if (!address.value) return false;

		let {
			full_address,
			name,
			phone
		} = address.value;
		form.address_id = address.value.id;
		region.value.region_text = full_address;
		region.value.name = name;
		region.value.phone = phone;
	};

	const overlimit = (type) => {
		if (type == "minus") {
			uni.$uv.toast("已经是最小值了");
		}
		if (type == "plus") {
			uni.$uv.toast("已经是最大值了");
		}
	};
	const submit = async () => {
		let res = await underwriteReceive({
			id: id.value,
			...form
		});
		form.value = '';
		uni.$uv.toast("操作成功");
		setTimeout(() => {
			uni.switchTab({
				url: '/pages/tabbar/my'
			})
		}, 1500);
	};
	onLoad(options => id.value = options.id);
	onShow(() => getDeta());
	onUnload(() => useAddressStore.removeAddress());
</script>

<style scoped lang="scss">
	.confirm_give {
		height: calc(100vh - var(--window-top) - var(--window-bottom));
		background-color: #f5f5f5;

		.left_slot {
			image {
				width: 43rpx;
				height: 36rpx;
			}
		}

		.nav_title {
			color: #F5F5F5;
			font-size: 36rpx;
		}

		.scroll_view {
			height: calc(100vh - var(--window-top) - var(--window-bottom) - #{$navBarHeight} - 100rpx);
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.address {
				border-radius: 16rpx;
				padding: 44rpx 30rpx;
				box-sizing: border-box;
				background: #FFFFFF;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.zb {
					width: 70rpx;
					height: 70rpx;
				}

				.address_view {
					flex: 1;
					height: 70rpx;
					padding: 0 20rpx;
					box-sizing: border-box;
					overflow: hidden;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.address_info {
						display: flex;
						align-items: center;

						.name {
							color: #333333;
							font-size: 30rpx;
							font-weight: bold;
						}

						.phone {
							color: #999999;
							font-size: 24rpx;
							padding-left: 30rpx;
							box-sizing: border-box;
						}
					}

					.address_deta {
						color: #333333;
						font-size: 24rpx;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}

				.next {
					width: 19rpx;
					height: 30rpx;
				}
			}

			.goods_info {
				border-radius: 16rpx;
				margin: 20rpx 0;
				padding: 30rpx 30rpx 0;
				box-sizing: border-box;
				background: #FFFFFF;
				overflow: hidden;

				.goods_box {
					padding-bottom: 40rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;

					.cover {
						width: 200rpx;
						height: 200rpx;
						border-radius: 10rpx;
					}

					.goods_view {
						flex: 1;
						padding-left: 10rpx;
						box-sizing: border-box;
						overflow: hidden;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.goods_deta {
							.name {
								line-height: 36rpx;
								color: #333333;
								font-size: 30rpx;
								font-weight: bold;
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
							}

							.synopsis {
								color: #999999;
								font-size: 20rpx;
								padding-top: 18rpx;
								box-sizing: border-box;
							}
						}

						.price {
							display: flex;
							align-items: center;

							.unit {
								color: #CA1C1D;
								font-size: 32rpx;
								font-weight: bold;
							}

							.value {
								color: #CA1C1D;
								font-size: 50rpx;
								font-weight: bold;
							}
						}
					}
				}

				.leave {
					border-top: 2rpx solid #EEEEEE;
					padding: 26rpx 0;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #333333;
						font-size: 30rpx;
						font-weight: bold;
					}

					input {
						flex: 1;
						padding-left: 18rpx;
						box-sizing: border-box;
					}
				}
			}

			.cell_view {
				border-radius: 20rpx;
				background: #FFFFFF;
				overflow: hidden;

				.item {
					height: 88rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #333333;
						font-size: 30rpx;
						font-weight: bold;
					}

					.value {
						flex: 1;
						text-align: right;
						color: #333333;
						font-size: 30rpx;
						padding-right: 18rpx;
						box-sizing: border-box;

						&.red {
							color: #F34343;
						}
					}

					.icon {
						width: 20rpx;
						height: 20rpx;
					}
				}
			}

			.other {
				border-radius: 20rpx;
				margin-top: 20rpx;
				padding: 30rpx 30rpx 0;
				box-sizing: border-box;
				background: #FFFFFF;

				.other_head {
					padding-bottom: 20rpx;
					box-sizing: border-box;
					display: flex;

					image {
						width: 30rpx;
						height: 30rpx;
					}

					view {
						color: #333333;
						font-size: 30rpx;
						font-weight: bold;
						padding-left: 8rpx;
						box-sizing: border-box;
					}
				}

				.nikename {
					padding: 30rpx 0;
					box-sizing: border-box;
					display: flex;
					align-items: center;

					.key {
						color: #333333;
						font-size: 30rpx;
						font-weight: bold;
					}

					input {
						flex: 1;
						font-size: 30rpx;
						padding-left: 26rpx;
						box-sizing: border-box;
					}
				}
			}
		}

		.footer {
			width: 100%;
			height: 100rpx;
			padding: 20rpx 30rpx 10rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.number {
				display: flex;
				align-items: flex-end;

				.key {
					color: #999999;
					font-size: 22rpx;
				}

				.value {
					color: #CA1C1D;
					font-size: 50rpx;
					font-family: DIN Next LT Pro;
					font-weight: bold;
				}
			}

			.submit {
				width: 100%;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 26rpx;
				border-radius: 10rpx;
				background: #CA1C1D;
			}
		}
	}
</style>